package com.bgs.controller;


import com.alibaba.fastjson.JSONObject;
import com.bgs.client.OrderDetailClient;
import com.bgs.common.R;
import com.bgs.config.DirectConfig;
import com.bgs.pojo.Store;
import com.bgs.pojo.vo.OrderDetail;
import com.bgs.pojo.vo.StoreTow;
import com.bgs.pojo.vo.ZeroToReportAndOrderDetail;
import com.bgs.service.StoreService;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import com.bgs.pojo.Store;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;



/**
 * <p>
 * 店铺表 前端控制器
 * </p>
 *
 * @author Asia
 * @since 2021-09-14
 */
@RestController
@RequestMapping("/bgs/store")
public class StoreController {
    @Autowired
    private StoreService storeService;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private OrderDetailClient orderDetailClient;

//    @GetMapping("/zeroOrderDetailIdToStoreId")
//    public List zeroOrderDetailIdToStoreId(Integer orderDetailId){
//        List<Store> list = storeService.zeroOrderDetailIdToStoreId(orderDetailId);
//        return list;
//    }

    @GetMapping("zeroStoreIdToStore/{storeId}")
    public List<Store> zeroStoreIdToStore(@PathVariable("storeId") Integer storeId) {
        return storeService.zeroStoreIdToStore(storeId);
    }
    /**
     * 后台店铺审核
     * @param beforeUserName
     * @param storeName
     * @return
     */
    @GetMapping("/TowGettableData")
    public R TowGettableData(String beforeUserName,String storeName){
        List<Store> list = storeService.TowGettableData(beforeUserName,storeName);
        return R.ok().data("list",list);
    }


    //对被举报商品全查
    @PostMapping("zeroGetStoreToZeroToReportAndOrderDetail")
    public R zeroGetStoreToZeroToReportAndOrderDetail(@RequestBody List<ZeroToReportAndOrderDetail> zeroToReportAndOrderDetails){
        System.out.println(zeroToReportAndOrderDetails);
        return storeService.zeroGetStoreToZeroToReportAndOrderDetail(zeroToReportAndOrderDetails);
    }


    //对商铺进行违规封禁，状态为4
    @GetMapping("zeroStoreStatus4")
    public R zeroStoreStatus4(Integer storeId){
        return storeService.zeroStoreStatus4(storeId);
    }
    /**
     * 后台店铺审核通过
     * @param storeId
     * @return
     */
    @GetMapping("/TowDianShen")
    public R TowDianShen(Integer storeId){
        int i = storeService.TowDianShen(storeId);
        if(i>0){
            Map map = new HashMap();
            map.put("storeId",storeId);
            map.put("text","审核成功");

            Message message = new Message(JSONObject.toJSONString(map).getBytes());
            rabbitTemplate.convertAndSend(DirectConfig.DIRECT_EXCHANGE_NAME,
                    DirectConfig.DIRECT_EXCHANGE_ROUTING_KEY,
                    message);
        }
        return R.ok().data("i",i);
    }

    /**
     * 后台店铺审核未通过
     * @param storeId
     * @return
     */
    @GetMapping("/TowDianShenNo")
    public R TowDianShenNo(Integer storeId){
        int i = storeService.TowDianShenNo(storeId);
        if(i>0){
            Map map = new HashMap();
            map.put("storeId",storeId);
            map.put("text","审核未通过");

            Message message = new Message(JSONObject.toJSONString(map).getBytes());
            rabbitTemplate.convertAndSend(DirectConfig.DIRECT_EXCHANGE_NAME,
                    DirectConfig.DIRECT_EXCHANGE_ROUTING_KEY,
                    message);
        }
        return R.ok().data("i",i);
    }

    /**
     * 阿里云上上传图片
     * @param file
     * @return
     */
    @PostMapping("/FourOpenImg")
    public String FourOpenImg(@RequestBody MultipartFile file){
        return storeService.FourOpenImg(file);
    }

    /**
     * 开启店铺
     * @param store
     * @return
     */
    @PostMapping("/FourOpenShop")
    public R FourOpenShop(@RequestBody Store store){
        return storeService.FourOpenShop(store);
    }
    /**
     * 被订单服务调用查询店铺名字
     */
    @GetMapping("/TowOrderStore")
    public List<Store> TowOrderStore(Integer storeId){
        List<Store> list = storeService.TowOrderStore(storeId);
        return list;
    }

    /**
     * 查询当前商铺的所有商品
     * @param beforeUserId
     * @return
     */
    @GetMapping("/TowListCircle")
    public R TowListCircle(Integer beforeUserId){
        List<StoreTow> list = storeService.TowListCircle(beforeUserId);
        list.stream().forEach(x->{
            List<OrderDetail> list1 = orderDetailClient.TowOrderList(x.getStoreId());
            x.setOrderDetails(list1);
        });
        return R.ok().data("list",list);
    }

    @GetMapping("/FourStoreId")
    public R fourStoreId(int id){
        return storeService.fourStoreId(id);
    }

    /**/
    @GetMapping("/FourStore")
    public List<Store> FourStore(){
        return storeService.FourStore();
    }

}

